package com.baselibrary.bigimage;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.baselibrary.glideimage.DisplayUtil;
import com.baselibrary.glideimage.GlideImageLoader;
import com.baselibrary.glideimage.progress.CircleProgressView;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.github.chrisbanes.photoview.PhotoView;
import com.quanshu.exchange.R;
import com.quanshu.exchange.ui.base.BaseFragment;

import butterknife.BindView;

/**
 * Created by jye on 2017/9/20.
 */

public class ImageLoadFragment extends BaseFragment {

    @BindView(R.id.photoView)
    PhotoView photoView;
    @BindView(R.id.progressView)
    CircleProgressView progressView;

    public static ImageLoadFragment newInstance(String str) {
        ImageLoadFragment f = new ImageLoadFragment();
        Bundle args = new Bundle();
        args.putSerializable("url", str);
        f.setArguments(args);
        return f;
    }

    @Override
    public int setFragmentLayoutID() {
        return R.layout.item_photoview;
    }

    @Override
    protected void initView() {

    }


    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

        String url = (String) getArguments().getSerializable("url");

        photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        photoView.setOnPhotoTapListener((view, x, y) -> getActivity().finish());
        photoView.setOnOutsidePhotoTapListener(imageView -> getActivity().finish());

        GlideImageLoader imageLoader = GlideImageLoader.create(photoView);
        imageLoader.setOnGlideImageViewListener(url, (percent, isDone, exception) -> {
            if (progressView != null) {
                progressView.setProgress(percent);
                progressView.setVisibility(isDone ? View.GONE : View.VISIBLE);
            }
        });
        RequestOptions requestOptions = imageLoader.requestOptions(R.color.placeholder)
                .centerCrop()
                .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
        RequestBuilder<Drawable> requestBuilder = imageLoader.requestBuilder(url, requestOptions)
                .transition(DrawableTransitionOptions.withCrossFade());
        requestBuilder.into(new SimpleTarget<Drawable>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
            @Override
            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                if (resource.getIntrinsicHeight() > DisplayUtil.getScreenHeight(getActivity())) {
                    photoView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                }
                requestBuilder.into(photoView);
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
